package com.wswy.wzcx.module.weather

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import com.amap.api.services.weather.LocalWeatherForecastResult
import com.amap.api.services.weather.LocalWeatherLiveResult
import com.amap.api.services.weather.WeatherSearch
import com.amap.api.services.weather.WeatherSearchQuery
import com.blankj.utilcode.util.TimeUtils
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.model.reSend
import com.wswy.wzcx.model.weather.ForecastWeather
import com.wswy.wzcx.model.weather.LiveWeather
import com.wswy.wzcx.model.weather.WeatherData
import com.wswy.wzcx.module.LocManager
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*

object AmapWeatherManager : WeatherSearch.OnWeatherSearchListener {

    private const val TAG = "AmapWeatherManager"

    private val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)



    private val liveWeatherLiveData = MutableLiveData<WeatherData>().apply {
        value = WeatherData()
    }

    fun loadWeather(city:String,clear:Boolean = true){

        if (clear){
            liveWeatherLiveData.value?.reset()
        }

        Timber.e(" -->loadWeather $city")

        WeatherSearch(AppContext.getContext()).run {
            query = WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_LIVE)
            setOnWeatherSearchListener(this@AmapWeatherManager)
            searchWeatherAsyn()
        }

        WeatherSearch(AppContext.getContext()).run {
            query = WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_FORECAST)
            setOnWeatherSearchListener(this@AmapWeatherManager)
            searchWeatherAsyn()
        }



    }

    override fun onWeatherLiveSearched(localWeatherLiveResult: LocalWeatherLiveResult?, code: Int) {

        if (code == 1000 && localWeatherLiveResult != null) {
            val liveResult = localWeatherLiveResult.liveResult

            liveWeatherLiveData.value?.run {

                liveWeather = LiveWeather(
                        liveResult.city,liveResult.temperature,liveResult.weather,
                        liveResult.windPower,liveResult.windDirection,liveResult.humidity,
                        TimeUtils.string2Date(liveResult.reportTime)?:TimeUtils.getNowDate()
                )
            }



            liveWeatherLiveData.reSend()

        }
    }

    override fun onWeatherForecastSearched(localWeatherForecastResult: LocalWeatherForecastResult?, code: Int) {

        if(code == 1000 && localWeatherForecastResult != null){


            liveWeatherLiveData.value?.forecastResult = localWeatherForecastResult.forecastResult?.weatherForecast?.map {

//                Log.e(TAG, " --> ${it.date}   ${it.dayWeather}");
                ForecastWeather(sdf.parse(it.date),it.dayTemp,it.dayWeather,it.dayWindDirection,it.dayWindPower,
                        it.nightTemp,it.nightWeather,it.nightWindDirection,it.nightWindPower
                )
            }?.filter { it.date != null }?.sortedBy { it.date }

            liveWeatherLiveData.reSend()

        }

    }

    fun reloadWeather(){
        LocManager.getInstance().currentWZCity.name?.run {
            loadWeather(this,false)
        }
    }

    fun getWeatherLiveData():LiveData<WeatherData> = liveWeatherLiveData

}